package cn.wolfcode.luowowo.wensite.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;

import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.exception.LogicException;
import cn.wolfcode.luowowo.member.util.AjaxResult;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.wensite.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;


    @RequestMapping("")
    public String index(Model model) {
        List<StrategyStatisVO> vos = strategyStatisRedisService.listCommendVO();
        //abroadCds
        //国外
        ArrayList<StrategyStatisVO> abroadCds = new ArrayList<>();
        //unabroadCds
        //国内
        ArrayList<StrategyStatisVO> unabroadCds = new ArrayList<>();


        for (StrategyStatisVO vo : vos) {
            if (vo.isIsabroad()) {
                //国外
                if (abroadCds.size() < 10) {
                    abroadCds.add(vo);
                }
            } else {
                if (unabroadCds.size() < 10) {
                    unabroadCds.add(vo);
                }
            }
        }
        model.addAttribute("abroadCds", abroadCds);
        model.addAttribute("unabroadCds", unabroadCds);
        //commends
        model.addAttribute("commends", strategyCommendService.getTop5());
        //hotCds
        model.addAttribute("hotCds", strategyCommendService.getHotVOTop10());

        //themeCds
        List<Map<String, Object>> themeCds = strategyTemplateSearchService.queryThemeTop10();
        model.addAttribute("themeCds", themeCds);
        //themes
        List<StatisVO> themes = strategyTemplateSearchService.queryConditionThemeGroup();
        model.addAttribute("themes", themes);
        //chinas
        List<StatisVO> chinas = strategyTemplateSearchService.queryConditionProvincesGroup();
        model.addAttribute("chinas", chinas);

        //Abroads
        List<StatisVO> abroads = strategyTemplateSearchService.queryConditionAbroadsGroup();
        model.addAttribute("abroads", abroads);

        return "strategy/index";
    }

    @RequestMapping("/detail")

    public String detail(Model model, Long id) {
        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);

        model.addAttribute("detail", detail);


        //阅读数+1
        strategyStatisRedisService.viewnumIncrease(id, 1);
        model.addAttribute("vo", strategyStatisRedisService.getStrategyStatisVO(id));

        //热门攻略加分:
        strategyStatisRedisService.addHotScore(id, 1);
        return "strategy/detail";
    }

    //点击查看全部
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //这样拼接的话,先减删掉当前吐司导航地址,不然他会拼接2个当前目的地
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", destination);


        //tags  标签
        model.addAttribute("tags", strategyTagService.list());

        //pageinfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategy/list";
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo) {
        //判断评论用户是否登录
        if (userInfo == null) {
            throw new LogicException("请登录后,在进行操作");
        }
//        comment.setUserId(userInfo.getId());
//        comment.setHeadUrl(userInfo.getHeadImgUrl());
//        comment.setLevel(userInfo.getLevel());
//        comment.setCity(userInfo.getCity());
//        comment.setUsername(userInfo.getNickname());
//        strategyCommentService.save(comment);
//        return AjaxResult.SUCCESS.addData(1);


        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setCreateTime(new Date());
        strategyCommentService.save(comment);


        //评论数+1
        strategyStatisRedisService.replynumIncrease(comment.getDetailId(), 1);
        int replynum = strategyStatisRedisService.getStrategyStatisVO(comment.getDetailId()).getReplynum();


        //热门攻略加分
        strategyStatisRedisService.addHotScore(comment.getDetailId(), 1);

        return AjaxResult.SUCCESS.addData(replynum);
    }

    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo) {
        Page<StrategyComment> page = strategyCommentService.query(qo);
        model.addAttribute("page", page);
        return "strategy/commentTpl";
    }

    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumubUp(String toid, Long fromid) {
        strategyCommentService.commentThumubUp(toid, fromid);
        return AjaxResult.SUCCESS;
    }

    //攻略收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请登录后操作");
            result.setCode(AjaxResult.RESPONSE_CODE_NOLOGIN);
            return result;
        }
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());

        //攻略推荐排行加分
        strategyStatisRedisService.addCommendScore(sid, ret ? 1 : -1);

        return new AjaxResult(ret, null).addData(strategyStatisRedisService.getStrategyStatisVO(sid));
    }


    //攻略点赞
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请登录后操作");
            result.setCode(AjaxResult.RESPONSE_CODE_NOLOGIN);
            return result;
        }
        boolean ret = strategyStatisRedisService.thumbup(sid, userInfo.getId());
        return new AjaxResult(ret, null).addData(strategyStatisRedisService.getStrategyStatisVO(sid));
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        Page page = strategyTemplateSearchService.query(qo);
        model.addAttribute("page", page);
        return "strategy/searchPageTpl";
    }


}


